// Copyright (C) 2025, ZWSOFT Co., LTD.(Guangzhou)
// Licensed under the MIT License. See LICENSE in the project root for license information.

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using TestClient.Domain;

namespace TestClient.ViewModel;

public partial class PlyDartTestViewModel : PageViewModelBase
{
    [ObservableProperty]
    private string surfaceId = string.Empty;
    [ObservableProperty]
    private string boundaryId = string.Empty;
    [ObservableProperty]
    private string point1 = string.Empty;
    [ObservableProperty]
    private string point2 = string.Empty;
    [ObservableProperty]
    private string point3 = string.Empty;
    [ObservableProperty]
    private string hintPoint = string.Empty;

    [ObservableProperty]
    private double width;

    [ObservableProperty]
    private string resultId = string.Empty;

    public IAsyncRelayCommand SelectSurfaceCommand { get; }
    public IAsyncRelayCommand SelectBoundaryCommand { get; }
    public IAsyncRelayCommand SelectPoint1Command { get; }
    public IAsyncRelayCommand SelectPoint2Command { get; }
    public IAsyncRelayCommand SelectPoint3Command { get; }
    public IAsyncRelayCommand SelectHintPointCommand { get; }

    public IAsyncRelayCommand CreateSlitDartCommand { get; }
    public IAsyncRelayCommand CreateVshapeDartCommand { get; }


    public PlyDartTestViewModel(VFAdapterWrapper wrapper, ISnackNotifyService notifyService) : base(wrapper, notifyService)
    {
        SelectSurfaceCommand = CreateAsyncRpcCommand(SelectSurfaceAsync);
        SelectBoundaryCommand = CreateAsyncRpcCommand(SelectBoundaryAsync);
        SelectPoint1Command = CreateAsyncRpcCommand(SelectPoint1Async);
        SelectPoint2Command = CreateAsyncRpcCommand(SelectPoint2Async);
        SelectPoint3Command = CreateAsyncRpcCommand(SelectPoint3Async);
        SelectHintPointCommand = CreateAsyncRpcCommand(SelectHintPointAsync);

        CreateSlitDartCommand = CreateAsyncRpcCommand(CreateSlitDartAsync);
        CreateVshapeDartCommand = CreateAsyncRpcCommand(CreateVshapeDartAsync);
    }

    private async Task SelectSurfaceAsync()
    {
        EnsureConnected();
        SurfaceId = await ViewModelHelpers.SelectEnt(Wrapper, "Surface");
    }

    private async Task SelectBoundaryAsync()
    {
        EnsureConnected();
        BoundaryId = await ViewModelHelpers.SelectEnt(Wrapper, "Curve");
    }

    private async Task SelectPoint1Async()
    {
        EnsureConnected();
        Point1 = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task SelectPoint2Async()
    {
        EnsureConnected();
        Point2 = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task SelectPoint3Async()
    {
        EnsureConnected();
        Point3 = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task SelectHintPointAsync()
    {
        EnsureConnected();
        HintPoint = await ViewModelHelpers.SelectEnt(Wrapper, "Point");
    }

    private async Task CreateSlitDartAsync()
    {
        EnsureConnected();
        ResultId = await Task.Run(() =>
        {
            var p1 = Wrapper.GetPointCoord(Point1);
            var p2 = Wrapper.GetPointCoord(Point2);
            var p3 = Wrapper.GetPointCoord(HintPoint);
            var tool = Wrapper.CreateSlitDart(SurfaceId, BoundaryId, Width, p1, p2);
            return Wrapper.CreateNewDartBoundary(SurfaceId, BoundaryId, tool, p3);
        });
    }
    private async Task CreateVshapeDartAsync()
    {
        EnsureConnected();
        ResultId = await Task.Run(() =>
        {
            var p1 = Wrapper.GetPointCoord(Point1);
            var p2 = Wrapper.GetPointCoord(Point2);
            var p3 = Wrapper.GetPointCoord(Point3);
            var p4 = Wrapper.GetPointCoord(HintPoint);
            var tool = Wrapper.CreateVshapeDart(SurfaceId, BoundaryId, p1, p2, p3);
            return Wrapper.CreateNewDartBoundary(SurfaceId, BoundaryId, tool, p4);
        });
    }
}
